$(function () {
    var ue;

    function ContentEditor() {
        // 表格ID
        this.$form = $("#save-form");
        // 按钮
        this.$submit = $('#submit');
        this.editorId = 'editor';
        this.contentId = $('#contentId').val();

        // 初始化
        this.init();
    }

    ContentEditor.prototype.Constructor = ContentEditor;

    ContentEditor.prototype.init = function () {
        ue = UE.getEditor(this.editorId, {
            iframeCssUrl: '/assets/css/ueditor.css'
        });
        ue.ready(function() {
            ue.setContent($('#content').val());
        });
        this.registEvent();
    };

    ContentEditor.prototype.registEvent = function () {
        var that = this;
        this.$submit.click(function () {
            var title = $.trim($('#title').val());
            if (!title) {
                $('#title').focus();
                BootBox.warning('请输入标题！');
                return false;
            }
            if (title.length > 50) {
                $('#title').focus();
                BootBox.warning('标题最多支持50个字符！');
                return false;
            }
            if (!(UE.getEditor(that.editorId).hasContents())) {
                UE.getEditor(that.editorId).focus();
                BootBox.warning("请输入内容！");
                return false;
            }
            $('#content').val(UE.getEditor(that.editorId).getContent());
            SysTool.Ajax.ajaxSubmitForm(that.$form, function (data) {
                // PopMessage.success('保存成功', function () {
                    window.location.href = "/content/";
                // });
            });
        });
    };

    window.ContentEditer = new ContentEditor();

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function(action) {
        if (action == 'config') {
            return '/ueditor/config';
        } else if (action == 'uploadImage') {
            return '/ueditor/uploadImage';
        } else if (action == 'uploadFile') {
            return '/ueditor/uploadFile';
        } else if (action == 'listImage') {
            return '/ueditor/listImage';
        }else if (action == 'listFile') {
            return '/ueditor/listFile';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };
});